package com.tanhua.app.controller;

import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.app.service.UsersService;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {


    @Autowired
    private UsersService usersService;

    //用户资料--读取
    //Get /users
    @GetMapping
//    public ResponseEntity findUserInfo(@RequestParam Long userID,
//                                       @RequestHeader("Authorization")String token){
    public ResponseEntity findUserInfo(Long userID) {

//        //1.校验用户是否登录
//        boolean verifyToken = JwtUtils.verifyToken(token);
//        if (!verifyToken){
//            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
//        }
//        //2.解析token 获取用户主键id
//        Claims claims = JwtUtils.getClaims(token);
//        Long id = Long.parseLong(claims.get("id").toString());
        if (userID == null) {
            userID = UserHolder.getUserId();
        }
        //3.调用service实现功能
        UserInfoVo vo = usersService.findUserInfo(userID);
        //4.返回结果
        return ResponseEntity.ok(vo);
    }

    //用户资料 --修改
    //put //users
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo,
                                         @RequestHeader("Authorization") String token) {
        //1.校验用户是否登录
        boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
        }
        //2.解析token 获取用户主键id
        Claims claims = JwtUtils.getClaims(token);
        Long id = Long.parseLong(claims.get("id").toString());
        //3.把id设置到UserInfo中
        userInfo.setId(id);
        //4.调用service实现业务逻辑
        usersService.updateUserInfo(userInfo);
        //5.返回结果
        return ResponseEntity.ok(null);
    }


    //用户通用设置---读取
    //GET//users//settings
    @GetMapping("/settings")
    public ResponseEntity findSettings() {
        //1.调用service实现业务逻辑
        SettingsVo vo = usersService.findSettings();
        //2.返回结果
        return ResponseEntity.ok(vo);
    }

    //设置陌生人问题--保存
    //POST//users//questions
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {
        //1.获取参数  就是问题
        String content = (String) map.get("content");
        //2.调用service实现业务逻辑
        usersService.questions(content);
        //3.返回结果
        return ResponseEntity.ok(null);
    }

    //通知设置--保存
    //POST/users/notifications/setting
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map param) {
        //1.调用service实现业务逻辑
        usersService.notifications(param);
        //2.返回结果
        return ResponseEntity.ok(null);
    }

    //黑名单 - 翻页列表
    //GET /users/blacklist
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        //1. 调用Service实现业务逻辑
        PageResult pageResult = usersService.blacklist(page, pagesize);

        //2. 返回结果
        return ResponseEntity.ok(pageResult);
    }

    //黑名单 - 移除
    //DELETE /users/blacklist/:uid
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlacklist(@PathVariable("uid") Long uid) {
        //1. 调用Service实现业务逻辑
        usersService.deleteBlacklist(uid);

        //2. 返回结果
        return ResponseEntity.ok(null);
    }

    //用户资料 - 更新头像
    //POST/users/header
    @PostMapping("/header")
    public ResponseEntity header(MultipartFile headPhoto) throws IOException {
        Long id = UserHolder.getUserId();
        usersService.updateHead(headPhoto, id);
        return ResponseEntity.ok(null);
    }

    //    修改手机号- 1 发送短信验证码
//    POST  /users/phone/sendVerificationCode
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        //1.获取手机号
        String mobile = UserHolder.getMobile();
        //2.发送验证码
        usersService.sendVerificationCode(mobile);
        //3.返回数据
        return ResponseEntity.ok(null);

    }
        //    修改手机号 - 2 校验验证码
//    POST  /users/phone/checkVerificationCode
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map param) {
        //获取验证码
        String verificationCode = (String) param.get("verificationCode");
        //调方法
        boolean flag = usersService.checkVerificationCode(verificationCode);
        //封装结果
        Map retMap = new HashMap();
        retMap.put("verification",flag);

        return ResponseEntity.ok(retMap);
    }

    //    修改手机号 = 3 保存
//    POST  /users/phone
    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map param) {
        //1.获取参数
        String phone = (String) param.get("phone");
        //2.调用方法
        usersService.updatePhone(phone);
        //3.返回数据
        return ResponseEntity.ok(null);
    }
}
